package com.william.jdbcplus.boot.test.dao.impl;

import com.william.jdbcplus.boot.starter.wrap.QueryLambdaBootstrap;
import com.william.jdbcplus.boot.test.dao.SysDepartDao;
import com.william.jdbcplus.boot.test.entity.SysDepart;
import com.william.jdbcplus.boot.test.entity.SysUser;
import com.william.jdbcplus.boot.test.entity.SysUserDepart;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@AllArgsConstructor
public class SysDepartDaoImpl implements SysDepartDao {
    @Override
    public List<SysDepart> queryUserDeparts(String userId) {
        // select * from sys_depart where id IN ( select dep_id from sys_user_depart where user_id = #{userId} )

        QueryLambdaBootstrap bootstrap = new QueryLambdaBootstrap();
        bootstrap
                .selectAll(SysDepart.class)
                .from(SysDepart.class)
                .in(SysDepart::getId, lb -> lb
                        .select(SysUserDepart::getDepId)
                        .from(SysUserDepart.class)
                        .eq(SysUserDepart::getUserId, userId)
                );

        return bootstrap.findList(SysDepart.class);
    }

    @Override
    public List<SysDepart> queryDepartsByUsername(String username) {
        //        SELECT *
        //                FROM sys_depart
        //        WHERE id IN (
        //                SELECT dep_id
        //                FROM sys_user_depart
        //                WHERE user_id = (
        //                        SELECT id
        //                FROM sys_user
        //                WHERE username = #{username}
        //            )
        //        )

        QueryLambdaBootstrap bootstrap = new QueryLambdaBootstrap();
        bootstrap
                .selectAll(SysDepart.class)
                .from(SysDepart.class)
                .in(SysDepart::getId, lb -> lb
                        .select(SysUserDepart::getDepId)
                        .from(SysUserDepart.class)
                        .eq(SysUserDepart::getUserId, e -> e
                                .select(SysUser::getId)
                                .from(SysUser.class)
                                .eq(SysUser::getUsername, username)
                        )
                );
        return bootstrap.findList(SysDepart.class);
    }

    @Override
    public String queryDepartIdByOrgCode(String orgCode) {
        // @Select("select id from sys_depart where org_code=#{orgCode}")
        QueryLambdaBootstrap bootstrap = new QueryLambdaBootstrap();
        bootstrap
                .select(SysDepart::getId)
                .from(SysDepart.class)
                .eq(SysDepart::getOrgCode, orgCode);

        String id = bootstrap.findOne(String.class);
        return id;
    }

    @Override
    public SysDepart getParentDepartId(String departId) {
        // @Select("select id,parent_id from sys_depart where id=#{departId}")
        QueryLambdaBootstrap bootstrap = new QueryLambdaBootstrap();
        bootstrap
                .select(SysDepart::getId, SysDepart::getParentId)
                .from(SysDepart.class)
                .eq(SysDepart::getId, departId);

        return bootstrap.findOne(SysDepart.class);
    }

    @Override
    public List<String> getSubDepIdsByDepId(String departId) {
        // select id from sys_depart where del_flag = '0' and org_code like concat((select org_code from sys_depart where id=#{departId}),'%')

        QueryLambdaBootstrap bootstrap = new QueryLambdaBootstrap();
        bootstrap
                .select(SysDepart::getOrgCode)
                .from(SysDepart.class)
                .eq(SysDepart::getId, departId);

        String orgCode = bootstrap.findOne(String.class);

        QueryLambdaBootstrap lambdaBootstrap = new QueryLambdaBootstrap();
        lambdaBootstrap
                .select(SysDepart::getId)
                .from(SysDepart.class)
                .eq(SysDepart::getDelFlag, "0")
                .likeLeft(SysDepart::getOrgCode, orgCode);

        return lambdaBootstrap.findList(String.class);
    }

    @Override
    public List<String> getSubDepIdsByOrgCodes(String[] orgCodes) {
        //        select id from sys_depart where del_flag = '0' and
        //                <foreach collection="orgCodes" item="item" index="index"  open="(" separator="or" close=")">
        //                org_code LIKE CONCAT(#{item},'%')
        //        </foreach>

        QueryLambdaBootstrap bootstrap = new QueryLambdaBootstrap();
        bootstrap
                .select(SysDepart::getId)
                .from(SysDepart.class)
                .eq(SysDepart::getDelFlag, "0");

        QueryLambdaBootstrap bootstrap2 = new QueryLambdaBootstrap();
        for (String orgCode : orgCodes) {
            bootstrap2.or().likeLeft(SysDepart::getOrgCode, orgCode);
        }
        String where = bootstrap2.getWhereSegment();
        bootstrap.connect("(" + where + ")");
        return bootstrap.findList(String.class);
    }
}
